package com.hpe.wx.performance.redis.ctrl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hpe.wx.performance.redis.domain.KeyValue;
import com.hpe.wx.performance.redis.domain.PerformanceObj;
import com.hpe.wx.performance.redis.domain.Series;
import com.hpe.wx.performance.redis.domain.Xaxis;
import com.hpe.wx.performance.redis.service.PerFormanceService;

@Controller
@RequestMapping(value = "/hpe/performance/queue")
public class QueueCtrl {
	private static Logger logger = Logger.getLogger(QueueCtrl.class.getSimpleName());
	@Autowired
	private PerFormanceService service;

	@RequestMapping(value = "/testPage", method = RequestMethod.GET)
	public String testPage(Locale locale, Model model, String jmxUrl) {
		model.addAttribute("jmxUrl", "欢迎进入");

		return "hpe/performance/testPage";
	}

	@ResponseBody
	@RequestMapping(value = "/getQueueData", method = RequestMethod.GET)
	public List<PerformanceObj> getQueueData(Locale locale, Model model, String jmxUrl, String dateStart,
			String dateEnd, String queueIP) {// jmxUrl
		String targetIp = queueIP;
		List<PerformanceObj> res = new ArrayList<>();

		// for(String tempIp : ipArr){
		PerformanceObj tmpPerformanceObj = new PerformanceObj();

		logger.info("startTime: " + dateStart + "  endTime: " + dateEnd);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		long scoreStart = 0;
		long scoreEnd = 0;
		try {
			if (!StringUtils.isEmpty(dateStart)) {
				scoreStart = sdf.parse(dateStart).getTime();
			} else {
				dateStart = "2015/10/01 00:00";
			}
			scoreEnd = 0;
			if (StringUtils.isEmpty(dateEnd)) {
				scoreEnd = (new Date()).getTime();
			} else {
				scoreEnd = sdf.parse(dateEnd).getTime();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		List<Object> queueData = service.getDataListByScore("QUEUE_SIZE:" + targetIp, scoreStart, scoreEnd, 0);

		List<Double> queueTimeStamp = service.getTimeDataListByScore("QUEUE_SIZE:" + targetIp, scoreStart, scoreEnd, 0);

		logger.info(tmpPerformanceObj);

		/**/// get Data
		List<Series> seriesList = new ArrayList<Series>();

		Xaxis xaxis = new Xaxis();
		xaxis.setType("category");

		if (queueTimeStamp.size() > 0) {
			List<String> formatXaxisData = new ArrayList<>();
			for (Double timeDouble : queueTimeStamp) {
				formatXaxisData.add(getFormatDatefromTimeStamp(Math.round(timeDouble), null));
			}
			xaxis.setData(formatXaxisData);
			List<Xaxis> xaxisData = new ArrayList<>();
			xaxisData.add(xaxis);
			tmpPerformanceObj.setXaxis(xaxisData);

			// queue index 0
			Series seriesQueue = new Series();
			seriesQueue.setName("QUEUESIZE");
			seriesQueue.setType("line");

			seriesQueue.setData(queueData);

			seriesList.add(seriesQueue);

			tmpPerformanceObj.setSeriesList(seriesList);
		} else {
			tmpPerformanceObj = null;
		}
		res.add(tmpPerformanceObj);
		// }
		return res;
	}

	@ResponseBody
	@RequestMapping(value = "/getListQueueData", method = RequestMethod.GET)
	public List<List<Object>> getListQueueData(Locale locale, Model model, String jmxUrl, String dateStart, String dateEnd,
			String queueIP) {// jmxUrl
		String targetIp = queueIP;
		List<List<Object>> res = new ArrayList<>();

		logger.info("startTime: " + dateStart + "  endTime: " + dateEnd);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		long scoreStart = 0;
		long scoreEnd = 0;
		try {
			if (!StringUtils.isEmpty(dateStart)) {
				scoreStart = sdf.parse(dateStart).getTime();
			} else {
				dateStart = "2015/10/01 00:00";
			}
			scoreEnd = 0;
			if (!StringUtils.isEmpty(dateEnd)) {
				scoreEnd = sdf.parse(dateEnd).getTime();
			} else {
				scoreEnd = (new Date()).getTime();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		// get all the queueSizeData
		List<Object> queueData = service.getDataListByScore("QUEUE_SIZE:" + targetIp, scoreStart, scoreEnd, 0);
		// get all the timestampData
		List<Double> queueTimeStamp = service.getTimeDataListByScore("QUEUE_SIZE:" + targetIp, scoreStart, scoreEnd, 0);

		if (queueTimeStamp.size() > 0) { 
			for (int i = 0; i < queueTimeStamp.size(); i++) { 
				Double timeDouble = queueTimeStamp.get(i);
				Double queueSize = (Double) queueData.get(i);
				
				List<Object> kv = new ArrayList<>();
				//kv.add(getFormatDatefromTimeStamp(Math.round(timeDouble), null));
				kv.add(new Date(Math.round(timeDouble)));
				kv.add(queueSize.intValue());
				System.out.println(kv);
				res.add(kv);
			}
		} else {
			res = null;
		}
		System.out.println(res.toString());
		return res;
	}

	private String getFormatDatefromTimeStamp(long timestamp, String format) {
		Date d = new Date(timestamp);
		if (StringUtils.isEmpty(format))
			format = "yy/MM/dd HH:mm";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(d);
	}

}
